package com.hangup.activity;

import java.io.UnsupportedEncodingException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.devil.bbsclient.R;
import com.hangup.net.ActivityKeys;
import com.hangup.util.NetWorkUtil;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WeatherActivity extends BBSActivity {
	private TextView tv;
	private View weatherView;

	private TextView dateV;
	private TextView today;
	private TextView tomorrow;
	private ImageView todayimage;
	private ImageView tomrrowimage;
	private String city;
	private SoapObject detail;

	private Dialog dialog;
	private Button rbt;

	Handler handler_ = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 1:
				dialog = NetWorkUtil.showDialog(WeatherActivity.this, null,
						"天气", "正在加载气象信息,请稍等");
				dialog.show();
				break;
			case 2:
				dialog.cancel();
				break;
			case 3:
				dateV.setText(city);
				today.setText(detail.getProperty(5).toString() + "\n"
						+ detail.getProperty(6).toString() + "\n"
						+ detail.getProperty(7).toString());
				tomorrow.setText(detail.getProperty(12).toString() + "\n"
						+ detail.getProperty(13).toString() + "\n"
						+ detail.getProperty(14).toString());
				todayimage.setImageResource(NetWorkUtil.getImage(detail
						.getProperty(8).toString()));
				tomrrowimage.setImageResource(NetWorkUtil.getImage(detail
						.getProperty(15).toString()));
			default:
				break;
			}
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		tv = (TextView) findViewById(R.id.titleText);
		tv.setText(R.string.tabhost_tab3);

		RelativeLayout rl = (RelativeLayout) findViewById(R.id.view_main);
		LayoutInflater inflater = getLayoutInflater();
		weatherView = inflater.inflate(R.layout.activity_weather, null);
		rl.addView(weatherView);

		dateV = (TextView) findViewById(R.id.date);
		today = (TextView) findViewById(R.id.today);
		tomorrow = (TextView) findViewById(R.id.tomorrow);
		todayimage = (ImageView) findViewById(R.id.todayimage);
		tomrrowimage = (ImageView) findViewById(R.id.tomrrowimage);
		if (NetWorkUtil.NetType(WeatherActivity.this) == null) {
			Toast.makeText(getApplicationContext(), R.string.net,
					Toast.LENGTH_LONG).show();
		} else {
			handler_.sendMessage(handler_.obtainMessage(1));
			city = ((PublicApplication) getApplication()).getCityName();
			getWeatherInfo(city);
		}

		rbt = (Button) findViewById(R.id.btRefresh);
		rbt.setOnClickListener(new refresh());
		
		findViewById(R.id.bbs_view).setVisibility(View.GONE);
	}

	public void getWeatherInfo(String cityName) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				getWeather(city);
			}
		}).start();
	}

	public void getWeather(String cityName) {
		try {
			SoapObject rpc = new SoapObject(ActivityKeys.NAMESPACE,
					ActivityKeys.METHOD_NAME);
			rpc.addProperty("theCityName", cityName);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			HttpTransportSE ht = new HttpTransportSE(ActivityKeys.URL);
			ht.debug = true;
			ht.call(ActivityKeys.SOAP_ACTION, envelope);
			detail = (SoapObject) envelope.getResponse();
			// int count = detail.getPropertyCount();
			// String[] arr = new String[count];
			// for (int i = 0; i < count; i++) {
			// arr[i] = detail.getProperty(i).toString();
			// }
			parseWeather(detail);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			handler_.sendMessage(handler_.obtainMessage(2));
		}
	}

	private void parseWeather(SoapObject detail)
			throws UnsupportedEncodingException {
		try {
			// dateV.setText(city);
			// today.setText(detail.getProperty(5).toString() + "\n"
			// + detail.getProperty(6).toString() + "\n"
			// + detail.getProperty(7).toString());
			// tomorrow.setText(detail.getProperty(12).toString() + "\n"
			// + detail.getProperty(13).toString() + "\n"
			// + detail.getProperty(14).toString());
			// todayimage.setImageResource(NetWorkUtil.getImage(detail
			// .getProperty(8).toString()));
			// tomrrowimage.setImageResource(NetWorkUtil.getImage(detail
			// .getProperty(15).toString()));
			handler_.sendMessage(handler_.obtainMessage(3));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			handler_.sendMessage(handler_.obtainMessage(2));
		}

	}

	private final class refresh implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (NetWorkUtil.NetType(WeatherActivity.this) == null) {
				Toast.makeText(getApplicationContext(), R.string.net,
						Toast.LENGTH_LONG).show();
				return;
			}
			handler_.sendMessage(handler_.obtainMessage(1));
			city = ((PublicApplication) getApplication()).getCityName();
			getWeatherInfo(city);
		}

	}

	@Override
	public void postLists() {
		// TODO Auto-generated method stub
	}

}
